package com.wanghaoran.mapper;

import com.wanghaoran.dto.AssetsBorrowPageDTO;
import com.wanghaoran.entity.Assets;
import com.wanghaoran.entity.AssetsBorrow;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wanghaoran
 */
@Repository
public interface AssetsBorrowMapper {

    @Insert("""
            insert into rms_assets_borrow
            (`fk_assets_id`, `fk_emp_id`, `count`, `info`, `borrow_time`, `expected_return_time`, 
            `return_time`, `version`, `deleted`, `created`, `updated`)
            values 
            (#{fkAssetsId}, #{fkEmpId}, #{count}, #{info}, #{borrowTime}, #{expectedReturnTime}, 
            #{returnTime}, #{version}, #{deleted}, #{created}, #{updated})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(AssetsBorrow assetsBorrow);

    @Results(
            id = "assetsBorrowResultMap",
            value = {
                    @Result(property = "id", column = "id", id = true),
                    @Result(property = "fkAssetsId", column = "fk_assets_id"),
                    @Result(property = "fkEmpId", column = "fk_emp_id"),
                    @Result(property = "assets", column = "fk_assets_id", one = @One(select = "com.wanghaoran.mapper.AssetsMapper.select")),
                    @Result(property = "emp", column = "fk_emp_id", one = @One(select = "com.wanghaoran.mapper.EmpMapper.select")),

            }
    )
    @Select("""
            select * from rms_assets_borrow t
            where t.id = #{param1} and t.deleted = 0
            """)
    AssetsBorrow select(Long id);

    @ResultMap("assetsBorrowResultMap")
    @Select("""
            <script>
            select * from rms_assets_borrow t
            <where>
                <if test="fkAssetsId != null">t.fk_assets_id = #{fkAssetsId} and </if>
                <if test="fkEmpId != null">t.fk_emp_id = #{fkEmpId} and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<AssetsBorrow> list(AssetsBorrowPageDTO dto);

    @Update("""
            <script>
            update rms_assets_borrow
            <set>
            <if test = 'fkAssetsId != null'> fk_assets_id = #{fkAssetsId}, </if>
            <if test = 'fkEmpId != null'> fk_emp_id = #{fkEmpId}, </if>
            <if test = 'count != null'> count = #{count}, </if>
            <if test = 'info != null'> info = #{info}, </if>
            <if test = 'borrowTime != null'> borrow_time = #{borrowTime}, </if>
            <if test = 'expectedReturnTime != null'> expected_return_time = #{expectedReturnTime}, </if>
            <if test = 'returnTime != null'> return_time = #{returnTime}, </if>
            <if test = 'info != null'> info = #{info}, </if>
            <if test = 'version != null'> version = #{version}, </if>
            <if test = 'deleted != null'> deleted = #{deleted}, </if>
            <if test = 'created != null'> created = #{created}, </if>
            <if test = 'updated != null'> updated = #{updated}, </if>
            version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """)
    int update(AssetsBorrow assetsBorrow);

    @Update("""
            update rms_assets_borrow set deleted = 1, updated = current_timestamp 
            where id = #{id} 
            """)
    int delete(Long id);

    @Update("""
            <script>
            update rms_assets_borrow set deleted = 1, updated = current_timestamp where
            <foreach collection="list" item="e" open="id in (" close=")" separator=",">
                ${e}
            </foreach>
            </script>
            
            """)
    int deleteBatch(List<Long> ids);
}
